package com.aim.controller.system;

import com.aim.common.annotion.Permission;
import com.aim.common.base.AjaxResult;
import com.aim.common.entity.ZTreeNode;
import com.aim.common.service.ZtreeNodeService;
import com.aim.common.util.*;
import com.aim.config.shiro.ShiroKit;
import com.aim.entity.system.SysDept;
import com.aim.service.system.SysDeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @AUTO 部门控制器
 * @Author AIM
 * @DATE 2019/4/26
 */
@Controller
@RequestMapping("/dept")
public class SysDeptController {

	private static String PREFIX = "system/dept/";

	@Autowired
	private SysDeptService deptService;
	@Autowired
	private ZtreeNodeService ztreeNodeService;

	/**
	 * 列表页面
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "list.html";
	}

	/**
	 * 查询列表
	 */
	@PostMapping("/list")
	@Permission
	@ResponseBody
	public Object list(HttpServletRequest request) {
		Map<String, Object> params = WebUtil.getRequestMap(request);
		PageHelper.startPage(PageUtil.getPageNum(params), PageUtil.getPageSize(params));
		List<SysDept> list = deptService.findList(params);
		PageInfo<SysDept> pageInfo = new PageInfo<SysDept>(list);
		return AjaxResult.success((int) pageInfo.getTotal(), list);
	}

	/**
	 * 获取部门的tree列表
	 */
	@RequestMapping(value = "/tree")
	@ResponseBody
	public Object tree() {
		List<ZTreeNode> tree = this.ztreeNodeService.deptZtree();
		tree.add(ZTreeNode.createParent());
		return AjaxResult.success(tree);
	}

	/**
	 * 添加编辑页面
	 */
	@RequestMapping(value = {"add", "update"})
	public String editView(String id, Model model) {
		if (OftenUtil.isNotEmpty(id)) {
			SysDept sysDept = deptService.get(ConverterUtils.toLong(id));
			model.addAttribute("dept", sysDept);
		}
		return PREFIX + "edit.html";
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@ResponseBody
	public Object save(SysDept dept) {
		SysDept sysDept = this.deptService.get(dept.getDeptId());
		if (null == sysDept) {
			dept.setCreateTime(new Date());
			dept.setCreateUser(ShiroKit.getUser().getUserId());
			this.deptService.save(dept);
		} else {
			BeanUtils.copyProperties(dept, sysDept);
			sysDept.setUpdateTime(new Date());
			sysDept.setUpdateUser(ShiroKit.getUser().getUserId());
			this.deptService.update(sysDept);
		}
		return AjaxResult.success();
	}

	/**
	 * 删除
	 *
	 * @param ids 删除ID集合
	 */
	@Permission
	@PostMapping("/del")
	@ResponseBody
	public Object del(String ids) {
		if (OftenUtil.isNotEmpty(ids)) {
			this.deptService.batchDelete(ids.split(","));
			return AjaxResult.success();
		} else {
			return AjaxResult.error("参数异常");
		}
	}
}
